﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.Student;
using StartsEAS_API.DAL.Student;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.Student
{
    public class StudentBLL : IStudentBLL
    {
        private readonly StarsEASContext _context;
        public StudentDAL _studentDal;
        public StudentBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _studentDal = new(_context);
        }

        public ResultUtils GetAllStudent()
        {
            return _studentDal.GetAllStudent();
        }

        public ResultUtils GetAllStudentByClassId(string classId)
        {
            IEnumerable<Model.Models.Student> students = _studentDal.GetAllStudentByClassId(classId);
            if (students == null) return new ResultUtils() { code = 500, message = "未查询到班级列表内的学生" };
            return new ResultUtils() { code = 200, message = students };
        }

        public ResultUtils GetStudent(string StuId)
        {
            Model.Models.Student student = _studentDal.GetStudent(StuId);
            if (student == null) return new ResultUtils()
            {
                code= 500,
                message = "查无该学号学生"
            };
            return new ResultUtils() { code = 200, message = student };
        }

        public ResultUtils GetStudentByClassId(string token)
        {
            IEnumerable<Model.Models.Student> students = _studentDal.GetStudentByClassId(token);
            if (students == null) return new ResultUtils()
            {
                code = 500,
                message = "该老师无学生"
            };
            return new ResultUtils() { code = 200, message = students };
        }

        public ResultUtils InsertStudent(JObject data)
        {
            string jsonStr = JsonConvert.SerializeObject(data);
            Model.Models.Student stu = JsonConvert.DeserializeObject<Model.Models.Student>(jsonStr);
            return _studentDal.InsertStudent(stu);

        }

        public ResultUtils UpdateStudent(JObject data)
        {
            string jsonStr = JsonConvert.SerializeObject(data);
            Model.Models.Student stu = JsonConvert.DeserializeObject<Model.Models.Student>(jsonStr);
            return _studentDal.UpdateStudent(stu);
        }
    }
}
